﻿using ArticleClientLib;
using eslib.FormTools;
using NLog;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 软文管理;

namespace 软件文管理
{
    public partial class FormArticleSync : Form
    {
        public FormArticleSync()
        {
            new FormSizeAutoChanger(this);
            InitializeComponent();
        }


        ArticleSyncSystem syncSys;

        void view(string msg)
           => Invoke(() => statusView.Text = msg);

        private void FormArticleSync_Load(object sender, EventArgs e)
        {
            redirectConsole();
            disableSync();
            disablePreview();

            _ = syncClassifly();
        }

        /// <summary>
        /// 控制台重定向
        /// </summary>
        private void redirectConsole()
        {
            ConsoleTextWriter cwriter = new(view);
            ConsoleRedirector.Redirect(cwriter);
        }

        private async Task syncClassifly()
        {
            view("正在同步软文分类..");

            syncSys = new();
            await syncSys.ClassiflySync();

            view("软文分类同步完成");


            //在控件中显示分类
            articleControl1.UpdateView(syncSys.ArticleRoot);
        }

        private void 返回ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UIDispach.ToStart();
        }



        #region 同步软文

        void enableSync()
        {
            Invoke(() =>
            {
                buttonSyncArticle.Enabled = true;
            });
        }

        void disableSync()
        {
            Invoke(() =>
            {
                buttonSyncArticle.Enabled = false;
            });
        }


        /// <summary>
        /// 分类选择改变
        /// </summary>
        /// <param name="c"></param>
        private void articleControl1_ClassiflySelectChangeEvent(Classifly c)
        {
            CurrentSelectedClass = c;

            //启用同步按钮
            enableSync();

            CurrentSelectedArticle = null;
            disablePreview();
            disableRemvoe();
        }


        /// <summary>
        /// 同步当前选中分类的软文
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSyncArticle_Click(object sender, EventArgs e)
        {
            disableSync();

            //开始同步
            _ = beginSyncArticle();
        }

        private async Task beginSyncArticle()
        {
            if (CurrentSelectedClass == null)
            {
                view("未选中分类，无法同步");
                enableSync();
                return;
            }

            view($"开始同步分类[{CurrentSelectedClass.ReadJson().name}]的软文..");
            await syncSys.ArticleSync(CurrentSelectedClass);

            //同步后更新显示
            articleControl1.UpdateArticle();
            enableSync();

            view("软文同步完成");
        }

        Classifly? CurrentSelectedClass = null;


        #endregion



        #region 软文预览

        /// <summary>
        /// 软文选择改变
        /// </summary>
        /// <param name="c"></param>
        private void articleControl1_ArticleSelectChangeEvent(LocalArticle c)
        {
            CurrentSelectedArticle = c;
            enablePreview();
            enableRemove(c.Article.title);
        }
        LocalArticle? CurrentSelectedArticle = null;

        void enablePreview()
        {
            buttonPreview.Text = $"软文预览[{CurrentSelectedArticle.Article.title}]";
            buttonPreview.Enabled = true;
        }

        void disablePreview()
        {
            buttonPreview.Enabled = false;
        }

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            ArticleServer.ArticlePreview(CurrentSelectedArticle);
        }

        #endregion


        #region 删除

        void enableRemove(string title)
        {
            buttonRemove.Text = $"删除软文[{title}]";
            buttonRemove.Enabled = true;
        }

        void disableRemvoe()
        {
            buttonRemove.Enabled = false;
        }


        private void buttonRemove_Click(object sender, EventArgs e)
        {
            disableRemvoe();
            _ = removeArticle();
        }

        private async Task removeArticle()
        {
            if (MessageBox.Show($"【慎重】确定删除软文[{CurrentSelectedArticle.Article.title}]?\r\n服务器的软文会同步删除！",
               "确认删除",
               MessageBoxButtons.YesNo
               ) == DialogResult.Yes)
            {
                //删除服务端软文
                var res = await syncSys.Remove(CurrentSelectedArticle);
                if (res)
                {
                    //删除本地目录
                    CurrentSelectedClass.Remove(CurrentSelectedArticle);

                    //刷新显示
                    articleControl1.UpdateArticle();
                }
            }
        }
        #endregion

        private void 刷新软文列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            articleControl1.UpdateArticle();
        }




    }
}
